package com.pesilo.mvc.view.component.mediator
{
	import com.pesilo.mvc.controller.ApplicationFacade;
	import com.pesilo.mvc.view.component.module.ShowContainerComponent;
	import com.pesilo.mvc.view.component.module.homepageModule.HomepageComponent;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class ShowContainerMediator extends Mediator implements IMediator
	{
		public static const NAME:String="ShowContainerMediator";
		public function ShowContainerMediator(viewComponent:Object)
		{
			super(NAME,viewComponent);
		}
		
		override public function onRegister():void{
			
		}
		
		protected function __addedToStageHandle(e:Event):void
		{
			if(!facade.retrieveMediator(HomepageMediator.NAME)&&view.homepageCmp){
				facade.registerMediator(new HomepageMediator(view.homepageCmp));
			}
		}
		
		override public function listNotificationInterests():Array{
			return[
				ApplicationFacade.HIDE_SHOW_CONTAINER_EVENT,
				ApplicationFacade.SHOW_HOMEPAGE_EVENT,	
			]
		}
		
		override public function handleNotification(notification:INotification):void{
			var data:Object=notification.getBody();
			switch(notification.getName()){
				case ApplicationFacade.SHOW_HOMEPAGE_EVENT:
					if(!view.homepageCmp){
						view.homepageCmp=new HomepageComponent();
						view.homepageCmp.addEventListener(Event.ADDED_TO_STAGE,__addedToStageHandle);
					}
					view.addChild(view.homepageCmp);
					break;
				
				case ApplicationFacade.HIDE_SHOW_CONTAINER_EVENT:{
					view.removeChild(view.homepageCmp);
					view.homepageCmp=null;
				}
			}
		}
		
		protected function get view():ShowContainerComponent{
			return viewComponent as ShowContainerComponent;
		}
	}
}